package cn.itcast.web.controller.cargo;
/**
 * -------------------------------------------------------
 * Copyright (c) 2020, kachu
 * All rights reserved.
 * <p>
 * FileName：
 * Description：描述
 * History：V1.0
 * 2020/10/7 19:25  kachu  Desc
 * -------------------------------------------------------
 */

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.*;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *@author kachu
 *@version v1.0
 */
@Controller
@RequestMapping("/cargo/finance")
public class FinanceController extends BaseController{

    /**
     * 远程调用财务报运单业务层对象
     */
    @Reference
    private FinanceService financeService;

    /**
     * 远程调用发票业务层对象
     */
    @Reference
    private InvoiceService invoiceService;

    /**
     * 远程调用装箱单业务层对象
     */
    @Reference
    private PackingService packingService;

    /**
     * 远程调用委托单业务层对象
     */
    @Reference
    private ShippingOrderService shippingService;

    /**
     * 远程调用报运单业务层对象
     */
    @Reference
    private ExportService exportService;

    /**
     * 分页查询
     *
     * @param pageNum  当前页
     * @param pageSize 页面大小
     * @return 列表页面路径
     */
    @RequestMapping("/list")
    private String list(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<Finance> pageInfo = financeService.findByPage(pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/finance/finance-list";
    }

    /**
     * 新建或更新
     *
     * @param finance 财务报运单对象
     * @return 财务报运单列表查询路径
     */
    @RequestMapping("/edit")
    private String edit(Finance finance) {
        if (StringUtils.isEmpty(finance.getId())) {
            financeService.save(finance);
        } else {
            financeService.update(finance);
        }
        return "redirect:/cargo/finance/list";
    }

    /**
     * 查看
     *
     * @param id 财务报运单id
     */
    @RequestMapping("/toView")
    private String toView(String id) {
        Finance finance = financeService.findById(id);
        request.setAttribute("finance", finance);
        return "cargo/finance/finance-view";
    }

    /**
     * 打印excel
     *
     * @param id 财务报运单id
     */
    @RequestMapping("/printExcel")
    private void printExcel(String id) throws Exception {

        Finance finance = financeService.findById(id);
        Invoice invoice = invoiceService.findById(finance.getInvoiceId());
        Packing packing = packingService.findById(invoice.getShippingId());
        ShippingOrder shipping = shippingService.findById(invoice.getShippingId());
        String exportIds = packing.getExportIds();
        List<String> edis = Arrays.asList(exportIds.split(","));


        //【获取excel模板,文件流】
        InputStream in = request.getSession()
                .getServletContext()
                .getResourceAsStream("/make/xlsprint/tFINANCE.xls");

        // 第一：导出第一行
        Workbook workbook = new XSSFWorkbook(in);
        // 【获取工作表】
        Sheet sheet = workbook.getSheetAt(0);
        //获取第二行
        Row row = sheet.getRow(1);
        //获取日期单元格
        Cell cell = row.getCell(4);
        cell.setCellValue(new Date());
        //获取发票号单元格
        cell = row.getCell(15);
        cell.setCellValue(finance.getInvoiceId());
        //获取第三行
        row = sheet.getRow(2);
        //获取合同或确认书号单元格
        cell = row.getCell(4);
        cell.setCellValue(finance.getExportNos());
        //获取信用证 L/C NO.单元格
        cell = row.getCell(11);
        cell.setCellValue(invoice.getScNo());
        //获取收货人及地址单元格
        row = sheet.getRow(3);
        cell = row.getCell(4);
        cell.setCellValue(finance.getConsignee());
        //获取备注单元格
        cell = row.getCell(13);
        cell.setCellValue(shipping.getRemark());
        //获取装运港单元格
        row = sheet.getRow(4);
        cell = row.getCell(2);
        cell.setCellValue(finance.getShipmentPort());
        //获取目的港单元格
        cell = row.getCell(5);
        cell.setCellValue(finance.getDestinationPort());
        //获取价格条件单元格
        cell = row.getCell(10);
        cell.setCellValue(invoice.getTradeTerms());


        // 【获取第7行样式】
        CellStyle[] cellStyles = new CellStyle[8];
        row = sheet.getRow(2);
        for (int i = 0; i < cellStyles.length; i++) {
            cellStyles[i] = row.getCell(i+1).getCellStyle();
        }

        //用于计算行号
        int index = 1;
        for (String exportId : edis) {
            Export export = exportService.findById(exportId);
            List<ExportProduct> productList = export.getExportProducts();

            for (ExportProduct product : productList) {

                row = sheet.createRow(7 + index);
                //获取货号单元格
                cell = row.createCell(1);
                cell.setCellValue(product.getId());
                cell.setCellStyle(cellStyles[1]);
                //获取厂家名称单元格
                cell = row.createCell(4);
                cell.setCellValue(product.getFactoryName());
                cell.setCellStyle(cellStyles[4]);
                //获取单位单元格
                cell = row.createCell(5);
                cell.setCellValue(product.getPackingUnit());
                cell.setCellStyle(cellStyles[5]);
                //获取数量单元格
                cell = row.createCell(6);
                cell.setCellValue(product.getCnumber());
                cell.setCellStyle(cellStyles[6]);
                //获取件数单元格
                cell = row.createCell(7);
                cell.setCellValue(product.getBoxNum());
                cell.setCellStyle(cellStyles[7]);
                //获取毛重单元格
                cell = row.createCell(8);
                cell.setCellValue(product.getGrossWeight());
                cell.setCellStyle(cellStyles[8]);
                //获取毛重单元格
                cell = row.createCell(9);
                cell.setCellValue(product.getNetWeight());
                cell.setCellStyle(cellStyles[9]);
                //获取长宽高单元格
                cell = row.createCell(10);
                cell.setCellValue(product.getSizeLength());
                cell.setCellStyle(cellStyles[10]);
                cell = row.createCell(11);
                cell.setCellValue(product.getSizeWidth());
                cell.setCellStyle(cellStyles[11]);
                cell = row.createCell(12);
                cell.setCellValue(product.getSizeHeight());
                cell.setCellStyle(cellStyles[12]);
                //获取出口单价单价单元格
                cell = row.createCell(13);
                cell.setCellValue(product.getPrice());
                cell.setCellStyle(cellStyles[13]);
                //获取金额单元格
                cell = row.createCell(14);
                cell.setCellValue(product.getPrice()*product.getCnumber());
                cell.setCellStyle(cellStyles[14]);
                //获取单价单元格
                cell = row.createCell(15);
                cell.setCellValue(product.getPrice());
                cell.setCellStyle(cellStyles[15]);
                //获取收购成本金额单元格
                cell = row.createCell(16);
                cell.setCellValue(product.getPrice()*product.getCnumber());
                cell.setCellStyle(cellStyles[16]);
                //获取税金单元格
                cell = row.createCell(17);
                cell.setCellValue(product.getTax()*product.getCnumber());
                cell.setCellStyle(cellStyles[17]);

                index++;
            }
        }

        // 第四：导出下载
        response.setCharacterEncoding("UTF-8");
        // 设置下载响应头（浏览器会以下载方式处理响应结果）
        response.setHeader("content-disposition","attachment;filename=export.xlsx");
        ServletOutputStream out = response.getOutputStream();
        // 把excel文件流，写入到response输出流，实现下载
        workbook.write(out);
        workbook.close();
        out.close();

    }



    /**
     * 打印pdf
     *
     * @param id 财务报运单id
     */
    @RequestMapping("/printPdf")
    private void printPdf(String id) {
        System.out.println("test");

    }

}